<template>
  <div class="stockDeviceType">
    <div class="card-table">
      <el-button
        style="margin-bottom: 12px"
        icon="el-icon-plus"
        type="primary"
        @click="dialogVisible1 = true"
        >新增</el-button
      >
      <el-table
        :header-cell-style="getRowClass"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column align="center" type="index" label="序号" width="60">
        </el-table-column>
        <el-table-column align="center" label="型号" width="width">
          <template slot-scope="{ row }">
            {{ row.dates }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="unit" label="备注" width="width">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-button @click="edit(row)" type="text">修改</el-button>
            <el-button
              style="margin-left: 0"
              @click="deleteOne(row)"
              type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :modal-append-to-body="false"
        title="添加型号"
        :visible.sync="dialogVisible1"
        width="30%"
      >
        <el-form
          size="small"
          ref="formCreate"
          :model="formCreate"
          :rules="formCreateRuls"
          label-width="80px"
        >
          <el-form-item label="型号" prop="ip">
            <el-input v-model="formCreate.ip"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="created()">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        :modal-append-to-body="false"
        title="修改规格型号"
        :visible.sync="dialogVisible2"
        width="30%"
      >
        <el-form
          size="small"
          ref="formCreate"
          :rules="formCreateRuls"
          :model="formCreate"
          label-width="80px"
        >
          <el-form-item label="型号" prop="ip">
            <el-input v-model="formCreate.ip"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="(dialogVisible2 = false), (formCreate.ip = '')"
            >取 消</el-button
          >
          <el-button type="primary" @click="edits()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  WhatModelgetAll,
  WhatModelcreate,
  WhatModelupdata,
  WhatModeldelete,
} from "@/api/stock/whatmodel.js";

export default {
  components: {},
  data() {
    return {
      dialogVisible1: false,
      dialogVisible2: false,
      formCreate: {
        ip: "",
      },
      rows: {},
      tableData: [],
      param: {},
      formCreateRuls: {
        ip: [{ required: true, message: "型号不能为空", trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      WhatModelgetAll().then((res) => {
        console.log(res.data.data.pageData);
        this.tableData = res.data.data.pageData;
      });
    },
    deleteOne(row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const params = {
            id: row._id,
          };
          WhatModeldelete(params).then((res) => {
            if (res.data.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getData();
            } else {
              this.$message({
                type: "warning",
                message: res.data.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    created() {
      const params = {
        dates: this.formCreate.ip,
      };
      console.log(params);
      WhatModelcreate(params).then((res) => {
        console.log(res);
        if (res.data.code === 200) {
          this.$message({
            type: "success",
            message: "创建成功",
          });
          this.getData();
          this.dialogVisible1 = false;
        } else {
          this.$message({
            type: "warning",
            message: res.data.message,
          });
        }
      });
    },
    edit(row) {
      this.rows = row;
      this.formCreate.ip = row.dates;
      this.dialogVisible2 = true;
    },
    edits() {
      const params = {
        dates: this.formCreate.ip,
        _id: this.rows._id,
      };
      console.log(params);
      WhatModelupdata(params).then((res) => {
        console.log(this.param, "修改后的");
        if (res.data.code === 200) {
          this.$message({
            type: "success",
            message: "修改成功",
          });
          this.getData();
          this.dialogVisible2 = false;
        } else {
          this.$message({
            type: "warning",
            message: res.data.message,
          });
        }
      });
    },
    // resetForm(formName) {
    //   this.dialogVisible1 = false;
    //   this.dialogVisible2 = false;

    // },
  },
};
</script>

<style  scoped></style>
